--
-- Author:      name
-- DateTime:    2018-04-23 17:20:15
-- Description: 处理大厅socket消息的服务

local skynet = require "skynet"
local log = require "Log"
local msgpack = require "msgpack_helper"
local Objects = require "Objects"
local Message = require "gate.Message"
local OtherNode = require "gate.OtherNode"
local User = require "gate.User"
local Login = require "gate.Login"
local Table = require "gate.Table"
local Logout = require "gate.Logout"
local Command = require "gate.Command"

local gate_manager = ...
local dbmanager = "dbmgr_service"

g_handler = {
	-- CMD = {},
	-- gate_manager = gate_manager,
    --container
	-- user_agents = {}, --user_agents[fd] = userInfo
}

g_objects = Objects.new()

local message 
local command

---------------------------------------------------------
-- Local
---------------------------------------------------------

--把消息放在队列中
local function queueReq(userInfo, cmd, args)	
	return userInfo.queue(message:dispatch(), userInfo, cmd, args)	
end


--此服务用到的对象
local function createObjects()
	--消息派发
	message = Message.new()
	g_objects:add(message)	
	
	--用户数据
	local userObj = User.new(message)	
	--处理服务间消息
	command = Command.new(message,userObj)
	--别的节点要监听的消息
	local otherNode = OtherNode.new(message)
	--处理登，php透伟
	local loginObj = Login.new(message,userObj)
	--登出处理
	local logoutObj = Logout.new(message,userObj)	
	--进入桌子消息处理
	local tableObj = Table.new(message,userObj)

	--可热更的对象
	g_objects:add(userObj)
	g_objects:add(command)	
	g_objects:add(otherNode)	
	g_objects:add(loginObj)	
	g_objects:add(logoutObj)
	g_objects:add(tableObj)
	g_objects:hotfix("gate")	

end

--从客户发来的消息
local function clientMessage(fd, address, sid, pack_name, pack_body)
	if not fd then
		return 
	end
	if not pack_name or not pack_body then 
		log.debug("____not pack_name or not pack_body___",pack_name,pack_body)
		skynet.call(gate_manager, 'lua', 'kick_player',fd)
		return 
	end
	local userInfo = command:getUserInfo(fd)		
	if not userInfo or not pack_body then 
		print('error##########user_agents no found userInfo, fd:',fd,userInfo,pack_name)
		
		skynet.call(gate_manager, 'lua', 'kick_player',fd)
		return 
	end
	--[[
	if userInfo.sid ~= sid then 
		print("____error__sid_______",userInfo.sid,sid)
		skynet.call(gate_manager, 'lua', 'kick_player',fd)
		return 
	end
	userInfo.sid = userInfo.sid + 1
	if userInfo.sid > 50000 then 
		userInfo.sid = 1
	end
	]]--
	-- assert(pack_body, 'error##########pack_body is empty from pack_id',pack_id)		
	userInfo.time = 0	
	for msg,args in pairs(pack_body) do
		local ok, result  = pcall(queueReq, userInfo, msg, args)
		if not ok then
			log.error(result)
		end
	end
end
---------------------------------------------------------
-- Global function
---------------------------------------------------------
function calldb(cmd,...)
	return skynet.call(dbmanager,'lua',cmd,...)
end

function senddb(cmd,...)
	skynet.send(dbmanager,'lua',cmd,...)
end



-----------------------------------------------------------------------------------------
--skyent
-----------------------------------------------------------------------------------------

skynet.start(function()

	createObjects()
	--socket消息
	skynet.register_protocol {
		name = "client",
		id = skynet.PTYPE_CLIENT,
		--消息解包
		unpack = msgpack.unpack_message,
		--从客户发来的消息
		dispatch = clientMessage,
	}
	--处理服务消息
	skynet.dispatch("lua", message:dispatchSelfHandle())
end)